﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jy123.BLL.Public;
using System.Data.SqlClient;

namespace Jy123.BLL.User
{
    public class Group
    {

        /// <summary>
        /// 分页返回最新群组讨论主题
        /// </summary>
        /// <param name="userId">当前用户的ID</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="field">查询的字段</param>
        /// <param name="sortField">排序字段</param>
        /// <param name="order">排序方式(true为ASC|false为DESC)</param>
        /// <returns>数据集(群组讨论主题列表)</returns>
        public DataSet PaginationGroupTopic_New(int userId, int currentpage, int pageSize, string field, string sortField)
        {
            Jy123.Model.MPagination mPagination = new Jy123.Model.MPagination();
            StringBuilder strFilter = new StringBuilder();
            strFilter.AppendFormat("(FGroup IN (SELECT FGroup FROM Group_Member WHERE (FUser = {0}))) AND (DiscussType = 0) AND (State = 0) AND (BelongTopicID = 0)", userId);
            
            mPagination.Tables = "V_User_GroupTopicList";
            mPagination.PrimaryKey = "ID";
            mPagination.Fields = field;
            mPagination.CurrentPage = currentpage;
            mPagination.Filter = strFilter.ToString();
            mPagination.PageSize = pageSize;
            mPagination.Sort = sortField;
            mPagination.Group = string.Empty;

            return Pagination.GetList(mPagination);
        }

        /// <summary>
        /// 分页返回当前用户加入的群组
        /// </summary>
        /// <param name="userId">当前用户的ID</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="field">查询的字段</param>
        /// <param name="sortField">排序字段</param>
        /// <param name="order">排序方式(true为ASC|false为DESC)</param>
        /// <returns>数据集(群组列表)</returns>
        public DataSet PaginationGroupListMyJoin(int userId, int currentpage, int pageSize, string field, string sortField)
        {
            Jy123.Model.MPagination mPagination = new Jy123.Model.MPagination();
            StringBuilder strFilter = new StringBuilder();
            strFilter.AppendFormat(" (FUserCreater = {0}) OR (FUserMember = {0})", userId);
            
            mPagination.Tables = "V_User_GroupList";
            mPagination.PrimaryKey = "ID";
            mPagination.Fields = field;
            mPagination.CurrentPage = currentpage;
            mPagination.Filter = strFilter.ToString();
            mPagination.PageSize = pageSize;
            mPagination.Sort = sortField;
            mPagination.Group = string.Empty;

            return Pagination.GetList(mPagination);
        }

        /// <summary>
        /// 分页返回当前用户创建的群组
        /// </summary>
        /// <param name="userId">当前用户的ID</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="field">查询的字段</param>
        /// <param name="sortField">排序字段</param>
        /// <param name="order">排序方式(true为ASC|false为DESC)</param>
        /// <returns>数据集(群组列表)</returns>
        public DataSet PaginationGroupListMyCreate(int userId, int currentpage, int pageSize, string field, string sortField)
        {
            Jy123.Model.MPagination mPagination = new Jy123.Model.MPagination();
            StringBuilder strFilter = new StringBuilder();
            strFilter.AppendFormat("FUserCreater = {0}", userId);
           
            mPagination.Tables = "V_User_GroupList";
            mPagination.PrimaryKey = "ID";
            mPagination.Fields = field;
            mPagination.CurrentPage = currentpage;
            mPagination.Filter = strFilter.ToString();
            mPagination.PageSize = pageSize;
            mPagination.Sort = sortField;
            mPagination.Group = string.Empty;

            return Pagination.GetList(mPagination);
        }

        /// <summary>
        /// 分页返回好友的群组
        /// </summary>
        /// <param name="userId">当前用户的ID</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="field">查询的字段</param>
        /// <param name="sortField">排序字段</param>
        /// <param name="order">排序方式(true为ASC|false为DESC)</param>
        /// <returns>数据集(群组列表)</returns>
        public DataSet PaginationGroupListFriend(int userId, int currentpage, int pageSize, string field, string sortField)
        {
            Jy123.Model.MPagination mPagination = new Jy123.Model.MPagination();
            StringBuilder strFilter = new StringBuilder();
            strFilter.AppendFormat(" State=0 and FUserCreater!={0} and  FUserCreater in (select FFriend from dbo.User_Friend where User_Friend.FUser={0})", userId);
            
            mPagination.Tables = "V_User_GroupList";
            mPagination.PrimaryKey = "ID";
            mPagination.Fields = field;
            mPagination.CurrentPage = currentpage;
            mPagination.Filter = strFilter.ToString();
            mPagination.PageSize = pageSize;
            mPagination.Sort = sortField;
            mPagination.Group = string.Empty;

            return Pagination.GetList(mPagination);
        }

        /// <summary>
        /// 删除群组
        /// </summary>
        /// <param name="groupID">群组ID</param>
        /// <returns>被影响行数</returns>
        public int DelGroup(int groupID)
        {
            new Jy123.BLL.User_Group().Delete(groupID);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Group_Discuss where FGroup = @GroupID  ");
            strSql.Append("delete from Group_Invite where FGroup = @GroupID  ");
            strSql.Append("delete from Group_Member where FGroup = @GroupID  ");

            SqlParameter[] parameters = { 
                                        new SqlParameter("@GroupID", SqlDbType.Int, 4)};
            parameters[0].Value = groupID;
            return DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 分页返回随便看的群组
        /// </summary>
        /// <param name="userId">当前用户</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>数据集(群组列表)</returns>
        public DataSet GroupRandon(int userId, int currentpage, int pageSize,out List<int?> counts)
        {
            Jy123.Model.MPagination mPagination = new Jy123.Model.MPagination();
            StringBuilder strFilter = new StringBuilder();
            strFilter.AppendFormat("(State = 0) AND (FUser <> {0})", userId);

            mPagination.Tables = "V_User_GroupRandon";
            mPagination.PrimaryKey = "ID";
            //mPagination.Fields = "NEWID() as NID, ID, GroupName, FUser, Members, Portrait, Bulletin," +
            //                            " (SELECT COUNT(ID) AS Expr1" +
            //                            " FROM Group_Discuss" +
            //                            " WHERE (DiscussType = 0) AND (State = 0) AND (User_Group.ID = FGroup)) AS Cnt";
            mPagination.Fields = "*";

            mPagination.CurrentPage = currentpage;
            mPagination.Filter = strFilter.ToString();
            mPagination.PageSize = pageSize;
            mPagination.Sort = "NID";
            mPagination.Group = string.Empty;

            return Pagination.GetList(mPagination,out counts);
        }
    }
}
